package com.modular.extra.fm.site;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import com.c.opt.ComOpt;
import com.c.opt.tool.ToolEntity;
import com.modular.extra.fm.FactoryFmOpt;
import com.modular.extra.fm.om.FmOmUserOpt;
import com.modular.extra.fm.om.bean.FmOmUserDo;
import com.modular.extra.fm.site.bean.FmSiteInfoDo;
import ntd.tool.abnormal.ComAbnormal;

public class FmSiteOpt extends ComOpt<FmSiteInfoDo> {

    public static final String SITE_INFO = "site_info";

    public FmSiteOpt() {
        super("site_info", FactoryFmOpt.DS_ID);
    }

    public void updatePos(FmSiteInfoDo fmSiteInfoDo) {
        if (StrUtil.isBlank(fmSiteInfoDo.getMn())) {
            throw new ComAbnormal("mn为空，请检查后重试...");
        }
        fmSiteInfoDo.setUpdatedAt(LocalDateTimeUtil.now());
        syncEntity(ToolEntity.toEntity(fmSiteInfoDo), new Entity(SITE_INFO).set("mn", fmSiteInfoDo.getMn()));
    }

    /**
     * 根据mn获取基站信息
     * @param mn
     * @return
     */
    public FmSiteInfoDo getByMn(String mn) {
        return query().eq("mn", mn).getBean();
    }

    /**
     * 根据mn获取运维负责人
     * @param mn
     * @return
     */
    public FmOmUserDo getOmUser(String mn) {
        FmSiteInfoDo byMn = getByMn(mn);
        if (ObjectUtil.isNull(byMn)) {
            return null;
        }
        return FactoryFmOpt.FM_OM_USER_OPT.getBeanById(byMn.getOmUserId());
    }

}
